﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace rInvoice
{
    public partial class Products : UserControl
    {
        ServerObject mServerObject = null;
        string messageText = string.Empty;

        public Products()
        {
            InitializeComponent();
            mServerObject = new ServerObject();
        }      

        private void FillGridView()
        {
            DataTable gvTable = mServerObject.SelectProductList(false);
            dataGridView1.DataSource = gvTable;
        }

        private void Products_Load(object sender, EventArgs e)
        {
            FillGridView();
        }

        private void selectButton_Click(object sender, EventArgs e)
        {
            FillGridView();
        }

        private void newButton_Click(object sender, EventArgs e)
        {
            ProductsInputPanel AddForm = new ProductsInputPanel();
            AddForm.CreateNew = true;

            DialogResult resultDialog = AddForm.ShowDialog();
            if (resultDialog == DialogResult.OK)
            {
                messageText = "Data was saved";
            }
            else
            {
                messageText = "Sorry, data was not saved";
            }
            MessageBox.Show(messageText);
            FillGridView();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                int mProductID = 0;

                string prID = dataGridView1.Rows[e.RowIndex].Cells["ProductID"].Value.ToString();

                int.TryParse(prID, out mProductID);
                DataRow rowData = mServerObject.SelectProductByID(mProductID);

                ProductsInputPanel productInputPanel = new ProductsInputPanel();

                productInputPanel.ProductID = mProductID;
                productInputPanel.Cod = (int)rowData["Cod"];
                productInputPanel.TypID = (int)rowData["TypID"];
                productInputPanel.ClassifierID = (int)rowData["ClassifierID"];
                productInputPanel.ProductNume = rowData["ProductName"].ToString();
                productInputPanel.Copyrignt = rowData["Copyright"].ToString();
                productInputPanel.CompanyProductorID = (int)rowData["CompanyProductID"];
                productInputPanel.ProductYear = (int)rowData["ProductYear"];
                productInputPanel.ProductMonth = (int)rowData["ProductMonth"];
                productInputPanel.ProductDay = (int)rowData["ProductDay"];
                productInputPanel.BarCod = rowData["BarCod"].ToString();
                productInputPanel.ProductNameShort = rowData["ProductNameShort"].ToString();


                try
                {
                    DialogResult resultDialog = productInputPanel.ShowDialog();
                    if (resultDialog == DialogResult.OK)
                    {
                        messageText = "Data was saved";
                    }
                    else
                    {
                        messageText = "Sorry, data was not saved";
                    }
                    MessageBox.Show(messageText);
                }
                finally
                {
                    FillGridView();
                }
            }
        }




    }
}
